<?php

class CmsController extends AdminController {

    public $parentUrl;

    public function init() {
        parent::init();
        $this->parentUrl = $this->module->id . '/' . Yii::app()->getController()->getId();
    }

    public function actionView($id) {
        try {
            $this->render('view', array(
                'model' => $this->loadModel($id), 'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionCreate() {
        try {
            $model = new Cms;
            if (isset($_POST['Cms'])) {
                $model->attributes = $_POST['Cms'];
                $model->validate();
                if (!$model->hasErrors()) {
                    $this->saveCms($model);
                    $this->redirect(Yii::app()->createAbsoluteUrl('systems/cms/index'));
                }
            }

            $this->render('create', array(
                'model' => $model,
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionUpdate($id) {
        try {
            $model = $this->loadModel($id);
            if (isset($_POST['Cms'])) {
                $model->attributes = $_POST['Cms'];
                $model->validate();
                if (!$model->hasErrors()) {
                    if ($model->save())
                        $this->redirect(array('view', 'id' => $model->id));
                }
            }

            $this->render('update', array(
                'model' => $model, 'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionDelete($id) {
        try {
            $model = $this->loadModel($id);
            if ($model) {
                if ($model->delete()) {
                    $this->redirect(Yii::app()->createAbsoluteUrl($this->parentUrl . '/index'));
                }
            } else
                throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionIndex() {
        try {
            $model = new Cms();
            $data = $model->getAll();
            $this->render('index', array(
                'data' => $data, 'parentUrl' => $this->parentUrl
            ));
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function loadModel($id) {
        try {
            $model = Cms::model()->findByPk($id);
            if ($model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
            return $model;
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    function saveCms($model) {
                $cms = new Cms;
                $cms->name = (isset($_POST['Cms']['name'])) ? $_POST['Cms']['name'] : '';
                $cms->description = (isset($_POST['Cms']['description'])) ? $_POST['Cms']['description'] : '';
                $cms->page_type = $_POST['Cms']['page_type'];
                $cms->save();
    }

}
